package com.zkingsoft.service.impl;

import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.constance.SystemErrorCode;
import com.zkingsoft.service.BizBusinessService;
import org.springframework.stereotype.Service;
import com.matrix.core.exception.GlobleException;
import com.zkingsoft.common.bean.SysUsers;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.dao.BizBusinessDao;
import org.springframework.beans.factory.annotation.Autowired;
import com.matrix.core.tools.ModelUtils;
import java.util.List;
import java.util.Map;
import com.zkingsoft.bean.BizBusiness;

/**
 * @description service接口实现类(商户表)
 * @author 姜友瑶
 * @date 2018-09-06 06:30
 */
@Service
public class  BizBusinessServiceImpl implements  BizBusinessService{

	
	@Autowired
	private BizBusinessDao bizBusinessDao;
	
	
	@Override
	public int add(BizBusiness bizBusiness){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		bizBusiness.setCreateBy(loginUser.getSuName());
		bizBusiness.setUpdateBy(loginUser.getSuName());
		bizBusiness.setBusId(UUIDUtil.getRandomID());
		return bizBusinessDao.insert(bizBusiness);
		
	}
	
	@Override
	public int batchAdd(List<BizBusiness>  bizBusinessList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizBusinessList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizBusinessList.size() ? bizBusinessList.size() : end;
			List<BizBusiness> insertList = bizBusinessList.subList(begin, end);
			num += bizBusinessDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizBusiness oldBizBusiness
	,BizBusiness newBizBusiness){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizBusiness, newBizBusiness)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizBusiness, newBizBusiness);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizBusiness);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("busId", oldBizBusiness.getBusId());
			bizBusinessDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizBusiness bizBusiness){
	
		return bizBusinessDao.updateByModel(bizBusiness);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizBusinessDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String busId){
	
		return bizBusinessDao.deleteById(busId);
	
	}
	
	@Override
	public int removeByModel(BizBusiness bizBusiness){
	
		return bizBusinessDao.deleteByModel(bizBusiness);
	
	}
	
	
	@Override
	public List<BizBusiness> findInPage(BizBusiness bizBusiness,  PaginationVO pageVo){
	
		return bizBusinessDao.selectInPage(bizBusiness , pageVo);
	
	}
	
	@Override
	public List<BizBusiness> findByModel(BizBusiness bizBusiness){
	
		return bizBusinessDao.selectByModel(bizBusiness);
	
	}
	
	@Override
	public int  findTotal(BizBusiness bizBusiness){
	
		return bizBusinessDao.selectTotalRecord(bizBusiness);
	
	}
	
	@Override
	public BizBusiness  findById(String busId){
	
		return bizBusinessDao.selectById(busId);
	
	}

   	
	
	
}